package com.banzhi.shop.entity.OnekeyShelf;

import com.banzhi.shop.MyApp;
import com.banzhi.shop.entity.OnekeyShelf.IOnekeyShelfModel;
import com.banzhi.shop.entity.OnekeyShelf.OnekeyShelf;
import com.banzhi.shop.entity.OnekeyShelf.OnekeyShelfCallBack;
import com.banzhi.shop.state.URLString;
import com.banzhi.shop.utils.LoadingDialogUtils;
import com.banzhi.shop.utils.LogUtils;
import com.banzhi.shop.utils.ToastUtils;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by JIANGFEI on 2018/4/13.
 */

public class OnekeyShelfImpl implements IOnekeyShelfModel {



    @Override
    public void getData(final OnekeyShelfCallBack callBack) {
        RequestParams requestParams = new RequestParams(URLString.hostUrl + URLString.OneKeyShelfUrl);
        requestParams.addBodyParameter("token", MyApp.loginMessage.getString("token", ""));
        LogUtils.d("ManagementImp33333", requestParams.toString());
        x.http().post(requestParams, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    if (jsonObject.getInt("code") == 0) {
                        Gson gson = new Gson();
                        OnekeyShelf onekeyShelf = gson.fromJson(result, OnekeyShelf.class);
                        callBack.onGetOnekeyShelfDataSuccess(onekeyShelf);
                    } else {
                        callBack.onGetOnekeyShelfDataError(jsonObject);
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable throwable, boolean b) {
                LogUtils.e("OneKeyShelfUrl", "---onError----" + throwable.toString());
                LoadingDialogUtils.dismiss();
                ToastUtils.makeToastShort("网络加载失败,请稍后再试(83)");

            }

            @Override
            public void onCancelled(CancelledException e) {

            }

            @Override
            public void onFinished() {

            }
        });

    }
}
